﻿/*
 * Copyright (C) 2023 - 2024 lchfj.cn
 * 
 * This file is part of FastestHost.
 * 
 * FastestHost is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * FastestHost is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with FastestHost. If not, see <https://www.gnu.org/licenses/>.
 */

using System.Diagnostics;
using System.Security.Principal;

namespace Cn.Lchfj.FastestHost
{
    internal static class RunHelper
    {

        public static T Run<T>(Func<Task<T>> func)
        {
            try
            {
                return Task.Run(func).ConfigureAwait(false).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                ConsoleHelper.ShowException(ex);
                throw;
            }
        }

        public static void Run(Func<Task> func)
        {
            try
            {
                Task.Run(func).ConfigureAwait(false).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                ConsoleHelper.ShowException(ex);
                throw;
            }
        }

        internal static void Run(Action func)
        {
            try
            {
                func();
            }
            catch (Exception ex)
            {
                ConsoleHelper.ShowException(ex);
                throw;
            }
        }

        internal static T Run<T>(Func<T> func)
        {
            try
            {
                return func();
            }
            catch (Exception ex)
            {
                ConsoleHelper.ShowException(ex);
                throw;
            }
        }

        internal static bool IsRunInUAC()
        {
            if (Environment.OSVersion.Version.Major < 6)
                return true;

            var identity = WindowsIdentity.GetCurrent();
            var principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }

        internal static Process RunProcessOutline(string filename, IEnumerable<string>? arguments = null)
        {
            var startInfo = new ProcessStartInfo()
            {
                FileName = filename,
                UseShellExecute = true,
                RedirectStandardError = false,
                RedirectStandardInput = false,
                RedirectStandardOutput = false,
                Verb = "runas"
            };

            if (arguments is not null)
            {
                foreach (var item in arguments)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                        startInfo.ArgumentList.Add(item);
                }
            }

            return Process.Start(startInfo)!;
        }

        internal static Process RunProcessInline(string filename, IEnumerable<string?>? arguments = null)
        {
            var startInfo = new ProcessStartInfo()
            {
                FileName = filename,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                CreateNoWindow = true,
                Verb = "runas"
            };

            if (arguments is not null)
            {
                foreach (var item in arguments)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                        startInfo.ArgumentList.Add(item);
                }
            }

            return Process.Start(startInfo)!;
        }

        internal static void ShowEndApplication()
        {
            ConsoleHelper.WriteLine("已完成，【Enter】键退出");
            ConsoleHelper.ReadLine();
        }
    }
}
